package com.jic.rest;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author: gaoly
 * @date: 2019/11/21
 * @remark:
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class RestResult<T> implements Serializable {

    private static final long serialVersionUID = 3523558943791146781L;
    private static final String SUCCESS_CODE = "0000";
    private static final String SUCCESS_MESSAGE = "调用成功";

    private String code;
    private String message;
    private T data;

    public static RestResult error(String code, String message) {
        return RestResult.builder()
                .code(code)
                .message(message)
                .build();
    }

    public static <T> RestResult<T> success(T data) {
        return RestResult.<T>builder()
                .code(SUCCESS_CODE)
                .message(SUCCESS_MESSAGE)
                .data(data)
                .build();
    }

    public static boolean checkIsSuccess(RestResult restResult) {
        if (restResult != null && SUCCESS_CODE.equals(restResult.getCode())) {
            return true;
        }
        return false;
    }
}
